import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { LoaderManager } from "../../../../../../com/gengine/resource/LoaderManager";
import { ObjCreate } from "../../../common/ObjCreate";
import { DeployPanelItem } from "../DeployPanelItem";
import { StrenShiftAttributePanel } from "../StrenShiftAttributePanel";
import { GlobalClass } from "../../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../../resource/ImagesConst";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { ImageInfo } from "../../../../../../com/gengine/resource/info/ImageInfo";
import { ItemData } from "../../../../resource/info/ItemData";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { MsgManager } from "../../../../manager/MsgManager";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { GLabel } from "../../../../../../com/mui/controls/GLabel";
import { EquipmentItemDataUtil } from "../../../common/EquipmentItemDataUtil";
type int = number;
//class EquipStrenExchangePanel
    
    export  class EquipStrenExchangePanel extends BaseDeployPanel
    {
        constructor()
        {
            this.posData = [{"x": 70, "y": 94, "label": Language.getString(60198), "canRemove": true, "noBoarder": false}, {"x": 240, "y": 94, "label": Language.getString(60198), "canRemove": true, "noBoarder": false}];
            this.buttonLabel = Language.getString(60208);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            super.createChildren();
            LoaderManager.instance.load("Equip_BigArrow.png", this.onBigArrowLoaded);
            ObjCreate.createLabel(Language.getString(60209), 64, 66, 55, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            ObjCreate.createLabel(Language.getString(60210), 234, 66, 55, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            var loc1=0;
            while (loc1 < this.posData.length) 
            {
                loc3 = this.posData[loc1];
                (loc4 = new DeployPanelItem(loc1, loc3.noBoarder)).posIndex = loc1;
                loc4.x = loc3["x"];
                loc4.y = loc3["y"];
                loc4.label = loc3["label"];
                if (loc3.canRemove) 
                {
                    loc4.mouseEnabled = true;
                    loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc4);
                allItem.push(loc4);
                ++loc1;
            }
            this._shiftAttriPanel = new StrenShiftAttributePanel();
            this._shiftAttriPanel.x = 21;
            this._shiftAttriPanel.y = 152;
            this.addChild(this._shiftAttriPanel);
            this._targetAttriPanel = new StrenShiftAttributePanel();
            this._targetAttriPanel.x = 195;
            this._targetAttriPanel.y = 152;
            this.addChild(this._targetAttriPanel);
            loc2 = GlobalClass.getBitmap(ImagesConst.EquipRecast_arrow);
            loc2.x = 156;
            loc2.y = 215;
            this.addChild(loc2);
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 307;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onBigArrowLoaded(arg1: ImageInfo): void
        {
            this.bigArrow = new Object /* flash.display.Bitmap */(arg1.bitmapData);
            this.bigArrow.x = 138;
            this.bigArrow.y = 88;
            this.addChild(this.bigArrow);
            return;
        }

        /* internal  */setAttributes(arg1: ItemData=null, arg2: ItemData=null): void
        {
            if (arg1 && arg2) 
            {
                this._shiftAttriPanel.setAttributes(arg1);
                this._targetAttriPanel.setAttributes(arg2);
            }
            else 
            {
                this._shiftAttriPanel.setAttributes(null);
                this._targetAttriPanel.setAttributes(null);
            }
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!this.checkCanShift()) 
            {
                return;
            }
            var loc1=arg1.target;
            switch (loc1) 
            {
                case confirmButton:
                {
                    this.onConfirm();
                    break;
                }
            }
            return;
        }

        /* internal  */onConfirm(): void
        {
            var loc1=new EquipUpdateParams();
            loc1.operationId = this.operationId;
            loc1.autoUpdate = false;
            loc1.equipPosType = allItem[1].itemData.posType;
            loc1.equipPosType2 = allItem[0].itemData.posType;
            loc1.equipUid = allItem[1].itemData.uid;
            loc1.equipUid2 = allItem[0].itemData.uid;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc1, false, true));
            return;
        }

        /* internal  */checkCanShift(): Boolean
        {
            if (allItem[0].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60211));
                return false;
            }
            if (allItem[1].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60212));
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            if (allItem[0].itemData && allItem[1].itemData) 
            {
                loc1 = EquipAdvanceConfig.instance.getStrenShiftCost(allItem[0].itemData.itemExInfo.strengthen);
                this.setAttributes(allItem[0].itemData, allItem[1].itemData);
            }
            else 
            {
                loc1 = 0;
                this.setAttributes();
            }
            if (this._isSuccessStatus) 
            {
                confirmButton.enabled = false;
            }
            else 
            {
                confirmButton.enabled = true;
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            var loc1=null;
            if (arg1.itemInfo.item.category == ECategory._ECategoryEquip) 
            {
                if (this._isSuccessStatus) 
                {
                    clearAllItem();
                    this._isSuccessStatus = false;
                }
                if (allItem[0].itemData) 
                {
                    if (!(arg1.itemInfo.item.career == allItem[0].itemData.itemInfo.item.career) || !(arg1.itemInfo.item.type == allItem[0].itemData.itemInfo.item.type)) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60214));
                        return;
                    }
                    if (arg1.itemExInfo.strengthen >= allItem[0].itemData.itemExInfo.strengthen) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60215));
                        return;
                    }
                    allItem[1].itemData = arg1;
                }
                else 
                {
                    if (arg1.itemExInfo.strengthen < 1) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60213));
                        return;
                    }
                    allItem[0].itemData = arg1;
                }
                this.updateStatus();
                loc1 = this.getEquipChoosed();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, loc1, false, true));
            }
            return;
        }

        /* internal  */getEquipChoosed(): Array<any>
        {
            var loc1=[];
            var loc2=0;
            while (loc2 < allItem.length) 
            {
                if (allItem[loc2].itemData) 
                {
                    loc1.push(allItem[loc2].itemData);
                }
                ++loc2;
            }
            return loc1;
        }

        public moveOutByIndex(arg1: int): void
        {
            var loc1=null;
            if (this._isSuccessStatus) 
            {
                this._isSuccessStatus = false;
                this.clear();
                return;
            }
            if (arg1 != 0) 
            {
                if (arg1 == 1) 
                {
                    allItem[arg1].itemData = null;
                    this.updateStatus();
                    loc1 = this.getEquipChoosed();
                    this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, loc1, false, true));
                }
            }
            else 
            {
                this.clear();
            }
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=0;
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60216));
            }
            else 
            {
                this._isSuccessStatus = true;
                loc1 = allItem[0].itemData ? allItem[0].itemData.itemExInfo.strengthen : 0;
                loc4 = arg3 as Array<any>;
                loc5 = 0;
                while (loc5 < allItem.length) 
                {
                    if (allItem[loc5].itemData) 
                    {
                        loc6 = 0;
                        while (loc6 < loc4.length) 
                        {
                            if ((loc3 = new ItemData(loc4[loc6])).uid == allItem[loc5].itemData.uid) 
                            {
                                if (loc5 == 1) 
                                {
                                    loc2 = loc3.itemExInfo.strengthen;
                                }
                                allItem[loc5].itemData = loc3;
                            }
                            ++loc6;
                        }
                    }
                    ++loc5;
                }
                addUpdateSuccessEffect("equipEffect_strengthen.swf", 192, 110);
                this.showResultTips(loc1, loc2);
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            }
            return;
        }

        /* internal  */showResultTips(arg1: int, arg2: int): void
        {
            if (arg2 >= arg1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60217));
            }
            else if (arg2 != (arg1 - 1)) 
            {
                if (arg2 <= arg1 - 2) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60220));
                    MsgManager.showRollTipsMsg(Language.getString(60221));
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60218));
                MsgManager.showRollTipsMsg(Language.getString(60219));
            }
            return;
        }

        /* internal */ /* var */_isSuccessStatus: Boolean=false;

        /* internal */ /* var */hintItem: GLabel;

        /* internal */ /* var */bigArrow: Object /* flash.display.Bitmap */;

        /* internal */ /* var */equipUtil: EquipmentItemDataUtil;

        /* internal */ /* var */operationId: int=12;

        /* internal */ /* var */_shiftAttriPanel: StrenShiftAttributePanel;

        /* internal */ /* var */_targetAttriPanel: StrenShiftAttributePanel;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;
    }
